import UIKit

//提前退出函数
//利用guard在某些条件下提前退出
func greetByMiddleName(fromFullName name:(first:String,
                                          middle:String?,
                                          last:String)){
    guard let middleName = name.middle else {
        print("Hey there!")
        return
    }
    
    print("Hey \(middleName)")
}

greetByMiddleName(fromFullName: ("Matt","Danger","Mathias"))

greetByMiddleName(fromFullName: ("Matt",nil,"Mathias"))


//函数类型
//函数实例可以赋值给常量和变量

func sortedEvenOddNumbers(numbers:[Int]) -> (evens:[Int],odds:[Int]){
    var evens = [Int]()
    var odds = [Int]()
    
    for number in numbers {
        if number % 2 == 0 {
            evens.append(number)
        }else{
            odds.append(number)
        }
    }
    return (evens,odds)
}

let evenOddFunction:([Int]) -> ([Int],[Int]) = sortedEvenOddNumbers


let result = evenOddFunction([1,2,3])
let result2 = sortedEvenOddNumbers(numbers: [1,2,3])
print(result)
print(result2)


//白银练习题
func siftBeans(list:[String]) -> (beans:[String],otherGroceries:[String]) {
    var beans = [String]()
    var otherGroceries:[String] = []
    
    for item in list {
        if item.hasSuffix("beans") {
            beans.append(item)
        }else{
            otherGroceries.append(item)
        }
    }
    return (beans,otherGroceries)
}

let beansResult = siftBeans(list: ["green beans",
                              "milk",
                              "black beans",
                              "pinto beans",
                              "apples"])

print(beansResult.beans)
print(beansResult.otherGroceries)

